/*
* Copyright (c) 2011 Connection Road.  All rights reserved.
* This program and the accompanying material are made available
* under the terms of the Eclipse Public Licnese v1.0 which is
* available at http://www.eclipse.org/legal/epl-v10.html
*/

using System;
using System.Windows.Forms;
using System.IO;
using CrSyncFilesForms.Properties;
using CrSyncFilesForms.Utils;


namespace CrSyncFilesForms {
    public partial class CrSyncUiLogViewer : Form
    {

        private string LogFileName { get; set; }

        public CrSyncUiLogViewer(string logFilePath, string windowTitle) {
            InitializeComponent();

            CancelButton = ButtonCloseSyncToyLogs;
            LogFileName = logFilePath;

            //Initialize name
            //Text = string.Format("{0}.log",Resources.TEXT_SHORT_NAME);
            Text = (string.IsNullOrEmpty(windowTitle)) ? "CRSyncFiles Log Viewer" : windowTitle;
            
            try
            {
                Stream logStream = new FileStream(LogFileName, FileMode.OpenOrCreate);
                var logReader = new StreamReader(logStream);
                TextLogs.Text = logReader.ReadToEnd();
                logReader.Close();
            }catch(Exception logReaderExeption){
                throw new ApplicationException(logReaderExeption.Message,logReaderExeption);
            }

            Shown += CallBackShown;
        }

        public override sealed string Text
        {
            get { return base.Text; }
            set { base.Text = value; }
        }

        private void CallBackClose(object sender, EventArgs e) {
             Close();
        }

         private void CallBackClearLogs(object sender, EventArgs e)
         {
             if(File.Exists(LogFileName)){
                 File.Delete(LogFileName);
                 TextLogs.Clear();
             }
         }

        protected virtual void CallBackShown(object sender, EventArgs args)
        {
            if(!string.IsNullOrEmpty(TextLogs.Text))
            {
                TextLogs.SelectionStart = TextLogs.Text.Length;
                TextLogs.ScrollToCaret();
            }
        }

        private void CallBackCrSyncUiLogViewerLoad(object sender, EventArgs e)
        {
            // Color scheme
            BackColor = CRSyncColors.LogWindowBackGround;
        }
    }
}
